package com.ttg.web.utils;

import com.ttg.common.enums.AccountDimTypeEnum;
import com.ttg.common.enums.AccountItemTypeEnum;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Description：
 * Author: chenyou
 * Date: 2025/10/9 13:57
 * Company: ttg
 * Copyright: Copyright (c) 2025
 */
public class AccountItemTypeUtil {
    //保存项目各类计算金额
    private static InheritableThreadLocal<Map<AccountItemTypeEnum, HashMap<String, BigDecimal>>> itemMap = new InheritableThreadLocal<>();

    private AccountItemTypeUtil(){}



    public static void putResult(AccountItemTypeEnum itemType, String subKey, BigDecimal subVal){
        Map<AccountItemTypeEnum, HashMap<String, BigDecimal>> itemMap = getItemMap();
        boolean isNullFlag = false;
        if(itemMap==null || itemMap.isEmpty()){
            itemMap = new HashMap<AccountItemTypeEnum, HashMap<String, BigDecimal>>();
        }

        HashMap<String,BigDecimal> subMap = itemMap.get(itemType);
        if(subMap==null){
            subMap = new HashMap<String,BigDecimal>();
            itemMap.put(itemType,subMap);
        }
        subMap.put(subKey,subVal);

        if(isNullFlag) {
            setItemMap(itemMap);
        }
    }

    public static BigDecimal getResult(AccountItemTypeEnum itemType, String subKey){
        Map<AccountItemTypeEnum, HashMap<String, BigDecimal>> itemMap = getItemMap();
        if(itemMap==null || itemMap.isEmpty()){
            return new BigDecimal(0);
        }

        HashMap<String,BigDecimal> subMap = itemMap.get(itemType);
        if(subMap==null){
            return new BigDecimal(0);
        }
        return subMap.get(subKey);
    }


    public static Map<AccountItemTypeEnum, HashMap<String, BigDecimal>> getItemMap(){
        return itemMap.get();
    }

    public static void setItemMap(Map<AccountItemTypeEnum, HashMap<String, BigDecimal>> paramap){
        itemMap.set(paramap);
    }

    public static void removeItemMap(){
        itemMap.remove();
    }

    public static void main(String[] args) {
        AccountItemTypeEnum itemType = AccountItemTypeEnum.综合付息;
        Map<AccountItemTypeEnum,HashMap<String,Object>> map1 = new ConcurrentHashMap<>();
        HashMap<String,Object> subMap = map1.get(itemType);
        if(subMap==null){
            subMap = new HashMap<String,Object>();
            map1.put(itemType,subMap);
        }
        subMap.put("income",new BigDecimal(100));

        System.out.println();

        HashMap<String,Object> subMap1 = map1.get(itemType);
        if(subMap1==null){
            subMap1 = new HashMap<String,Object>();
            map1.put(itemType,subMap1);
        }
        subMap1.put("out",22);
        System.out.println();
    }
}
